<?php

namespace App\Http\Controllers\Xml;

use App\User;
use Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use function Sabre\Xml\Deserializer\enum;
use function Sabre\Xml\Deserializer\keyValue;
use function Sabre\Xml\Deserializer\repeatingElements;
use Sabre\Xml\Reader;
use Sabre\Xml\Service;

class IndexController extends Controller
{
    public $data='';
    public $status='success';
    public $msg;
    public $user;

    public function __construct(Request $request)
    {
        header('Access-Control-Allow-Origin:*');
        $arr = file_get_contents('php://input');
        $this->common($arr);
    }

    public function common($arr){
        $reader = new Service();
        $list =  $reader->parse($arr);
        if (!array_has($list,'Envelope')){
            $this->status='failure';
            $this->msg='错误';
            return ;
        }
        $list = array_get($list,'Envelope');

        if (!array_has($list[0],'Body')){
            $this->status='failure';
            $this->msg='错误';
            return ;
        }
        $list = array_get($list[0],'Body');

        if (!array_has($list[0],'reply')){
            $this->status='failure';
            $this->msg='错误';
            return ;
        }
        $list = array_get($list[0],'reply');

        if (!array_has($list[0],'msg')){
            $this->status='failure';
            $this->msg='错误';
            return ;
        }
        $list = array_get($list[0],'msg');

        if (!array_has($list[0],'attributes.Username')){
            $this->status='failure';
            $this->msg='错误';
            return ;
        }
        $account = array_get($list[0],'attributes.Username');

        if (!array_has($list[0],'attributes.Password')){
            $this->status='failure';
            $this->msg='错误';
            return ;
        }
        $password = array_get($list[0],'attributes.Password');

        $info = Auth::attempt(['account' => $account, 'password' => $password]);
        if (!$info){
            $this->status='failure';
            $this->msg='账号密码错误';
            return ;
        }else{
            $this->user = Auth::user();
        }

        if (!array_has($list[0],'OnlineTicketingServiceQuery')){
            $this->status='failure';
            $this->msg='错误';
            return ;
        }
        $list = array_get($list[0],'OnlineTicketingServiceQuery');
        $this->data = $list[0];
    }


    public function query_error(){
        return ['status'=>$this->status,'msg'=>$this->msg,'data'=>$this->data];
    }

    public function index(){

        if ($this->status=='failure'){
            return $this->query_error();
        }

        if (!array_has($this->data,'attributes.Id')){
            return $this->query_error();
        }
        $id = array_get($this->data,'attributes.Id');
        switch ($id){
            case 'ID_QueryCinema':
            return $this->query_cinema();
            break;

            case 'ID_QueryCinema':
            return $this->query_cinema();
            break;

            case 'ID_QueryCinema':
            return $this->query_cinema();
            break;

            case 'ID_QueryCinema':
            return $this->query_cinema();
            break;

            case 'ID_QueryCinema':
            return $this->query_cinema();
            break;

            case 'ID_QueryCinema':
            return $this->query_cinema();
            break;

            case 'ID_QueryCinema':
            return $this->query_cinema();
            break;

            case 'ID_QueryCinema':
            return $this->query_cinema();
            break;

            case 'ID_QueryCinema':
            return $this->query_cinema();
            break;

            case 'ID_QueryCinema':
            return $this->query_cinema();
            break;

            case 'ID_QueryCinema':
            return $this->query_cinema();
            break;

            default:
            $this->query_error();
            break;
        }
    }



    //
    public function readTest(){
        $data = file_get_contents('php://input');
        $reader = new Service();
        $list =  $reader->parse($data);
        return $this->chuli($list);

    }

    public function reader(){
        return $this->data;
        return array_get($this->data,'attributes.CinemaCode');
    }

    public function install(){

    }
}
